package it.aekidna.cleverplatypus.gui.components.columnsbrowser
{
	import it.aekidna.cleverplatypus.domain.IHasChildren;
	import it.aekidna.cleverplatypus.utils.ArrayCollectionUtils;

	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;

	[Bindable]
	public class ColumnsBrowserPM
	{
		public var root : ColumnsBrowserColumnPM;

		private var _columsPMs : Array;

		private var _dataProvider : ArrayCollection;

		private var _selectedBranch : IHasChildren;

		private var _selectedItems : ArrayCollection = new ArrayCollection();


		public function ColumnsBrowserPM()
		{
			BindingUtils.bindProperty( this, "dataProvider", root, "dataProvider" );
			root = new ColumnsBrowserColumnPM( null, this );
			_columsPMs = [ root ];
		}


		public function set selectedBranch( inBranch : IHasChildren ) : void
		{
			_selectedBranch = inBranch;
		}


		public function get selectedBranch() : IHasChildren
		{
			return _selectedBranch;
		}


		public function get selectedColumn() : ColumnsBrowserColumnPM
		{
			return ColumnsBrowserColumnPM( _columsPMs[ _columsPMs.length - 1 ]);
		}


		public function get selectedItems() : ArrayCollection
		{
			return _selectedItems;
		}


		public function set selectedItems( inItems : ArrayCollection ) : void
		{
			ArrayCollectionUtils.setItemsFromList( _selectedItems, inItems );
		}


		public function clearFilters() : void
		{
			for each( var column : ColumnsBrowserColumnPM in _columsPMs )
			{
				column.dataProvider.filterFunction = null;
			}
		}


		public function get dataProvider() : ArrayCollection
		{
			return _dataProvider;
		}


		public function set dataProvider( inDataProvider : ArrayCollection ) : void
		{
			_dataProvider = inDataProvider;
		}


		public function createChild() : ColumnsBrowserColumnPM
		{
			var currentColumn : ColumnsBrowserColumnPM = ColumnsBrowserColumnPM( _columsPMs[ _columsPMs.length - 1 ]);

			if( currentColumn.selectedItems.length == 1 && currentColumn.selectedItems.getItemAt( 0 ) is IHasChildren )
			{
				currentColumn.child = new ColumnsBrowserColumnPM( currentColumn, this );
				currentColumn.child.dataProvider = IHasChildren( currentColumn.selectedItems.getItemAt( 0 )).children;

				for each( var filter : Filter in currentColumn.filters )
				{
					if( filter.recursive )
						currentColumn.child.setFilter( filter );
				}
				currentColumn.child.dataProvider.refresh();
				_columsPMs.push( currentColumn.child );
				return currentColumn.child;
			}
			return null;
		}


		public function destroyChild( inColumn : ColumnsBrowserColumnPM ) : void
		{
			var index : int = _columsPMs.indexOf( inColumn );

			if( index > -1 )
				_columsPMs.splice( index, _columsPMs.length - index );
			else
				index = _columsPMs.length > 1 ? _columsPMs.length - 1 : 1;
			var lastColumn : ColumnsBrowserColumnPM = ColumnsBrowserColumnPM( _columsPMs[ index - 1 ]);
			lastColumn.child = null;

			if( ArrayCollection( lastColumn.selectedItems ).length == 1 )
			{
				selectedBranch = ArrayCollection( lastColumn.selectedItems ).getItemAt( 0 ) as IHasChildren;
			}
			else
			{
				if( lastColumn.parent && ArrayCollection( lastColumn.parent.selectedItems ).length == 1 )
				{
					lastColumn = lastColumn.parent;
					selectedBranch = ArrayCollection( lastColumn.selectedItems ).getItemAt( 0 ) as IHasChildren;
				}
				else
				{
					selectedBranch = null;
				}
			}
		}


	}
}